package cn.ncist.health_backend.controller;

import cn.ncist.common.RespResult;
import cn.ncist.constant.MessageConstant;
import cn.ncist.constant.RedisConstant;
import cn.ncist.entity.QueryPageBean;
import cn.ncist.health_interface.service.SetMealService;
import cn.ncist.pojo.Setmeal;
import cn.ncist.utils.QiNiuUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Slf4j
@RestController
@RequestMapping("/setMeal")
public class SetMealController {

    @DubboReference
    private SetMealService setMealService;

    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;

    /**
     * 带条件分页查询
     * @param queryPageBean
     * @return
     */
    @PreAuthorize("hasAuthority('SETMEAL_QUERY')")
    @PostMapping("/page")
    public RespResult page(@RequestBody QueryPageBean queryPageBean){
        if(queryPageBean.getCurrentPage() == 0 || queryPageBean.getPageSize() == 0){
            queryPageBean.setPageSize(10);
            queryPageBean.setCurrentPage(1);
        }
        log.info(queryPageBean.getQueryString());
        try {
            Page<Setmeal> pageInfo = setMealService.findPage(queryPageBean);
            return new RespResult(true, MessageConstant.QUERY_SETMEAL_SUCCESS,pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new RespResult(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }

    /**
     * 图片上传到QiuNiu云
     * @param imgFile
     * @return
     */
    @RequestMapping("/upload")
    public RespResult upload(@RequestParam("imgFile") MultipartFile imgFile){
        try {
            String originalFilename = imgFile.getOriginalFilename();
            log.info(originalFilename);
            int splitIndex = originalFilename.lastIndexOf('.');
            //获取文件名后缀
            String suffix = originalFilename.substring(splitIndex + 1);
            //UUID生成随机的文件名
            String fileName = UUID.randomUUID().toString() + suffix;
            //将图片名称存入redis的set集合内
            SetOperations<Object, Object> opsForSet = redisTemplate.opsForSet();
            opsForSet.add(RedisConstant.SETMEAL_PIC_RESOURCES,fileName);
            log.info(fileName);
            QiNiuUtil.upload2Qiniu(imgFile.getBytes(),fileName);
            return new RespResult(true,MessageConstant.PIC_UPLOAD_SUCCESS,fileName);
        } catch (IOException e) {
            e.printStackTrace();
            return new RespResult(false,MessageConstant.PIC_UPLOAD_FAIL);
        }
    }

    /**
     * 添加套餐
     * @param setmeal
     * @return
     */
    @PreAuthorize("hasAuthority('SETMEAL_ADD')")
    @PostMapping("/add")
    public RespResult add(Integer[] checkGroupIds, @RequestBody Setmeal setmeal){
        try {
            setMealService.addSetMeal(setmeal,checkGroupIds);
            return new RespResult(true,MessageConstant.ADD_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new RespResult(false,MessageConstant.ADD_SETMEAL_FAIL);
        }
    }

    @GetMapping("/findCheckGroupIdsBySetMealId")
    public RespResult findCheckGroupIdsBySetMealId(Integer setMealId){
        try {
            List<Integer> checkGroupIds = setMealService.selectCheckGroupIdBySetMealId(setMealId);
            return new RespResult(true,"查询套餐id成功",checkGroupIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new RespResult(true,"查询套餐id失败");
        }
    }

    @PreAuthorize("hasAuthority('SETMEAL_EDIT')")
    @PostMapping("/update")
    public RespResult update(Integer[] checkGroupIds,@RequestBody Setmeal setmeal){
        try {
            setMealService.updateBySetMealId(checkGroupIds,setmeal);
            return new RespResult(true,"更新套餐数据成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new RespResult(false,"更新套餐数据失败");
        }
    }

    @PreAuthorize("hasAuthority('SETMEAL_DELETE')")
    @GetMapping("/delete")
    public RespResult delete(Integer id){
        try {
            setMealService.deleteSetMeal(id);
            return new RespResult(true,"删除套餐数据成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new RespResult(false,"删除套餐数据失败");
        }
    }
}
